package com.hlingsoft.cms.action;

import com.hlingsoft.cms.dao.*;

import com.hlingsoft.cms.entity.*;
import com.hlingsoft.cms.entity.vo.NewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by selbylei on 2015/5/12.
 */
@Controller
@RequestMapping("/likes")
public class LikesAction extends BaseAction {



    /**
     * 点赞
     *
     * @param myId
     * @param newsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "myId") long myId,
            @RequestParam(value = "newsId") Integer newsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Date now = new Date();
            if (likesDao.getLikesByNewsId(myId, newsId) == null) {
                News news = newsDao.getNewsById(newsId);
                if (news!=null){
                    Likes likes = new Likes();
                    likes.setMyId(myId);
                    likes.setToId(news.getAppUserId());
                    likes.setNewsId(newsId);
                    likes.setCreateTime(now);
                    likes.setUpdateTime(now);
                    likesDao.addLikes(likes);

                    if (myId != news.getAppUserId()) {
                        LikesMessage likesMessage = new LikesMessage();
                        likesMessage.setMyId(myId);
                        likesMessage.setToId(news.getAppUserId());
                        likesMessage.setNewsId(newsId);
                        likesMessage.setCreateTime(now);
                        likesMessage.setUpdateTime(now);
                        likesMessageDao.addLikesMessage(likesMessage);

                        MessageStatus messageStatus = new MessageStatus();
                        messageStatus.setAppUserId(likes.getToId());
                        messageStatus.setMessageId(likesMessage.getId());
                        messageStatus.setMessageType(2);
                        messageStatusDao.addMessageStatus(messageStatus);
                    }
                    resultMap.put("likes", likes);

                    //返回信息
                    NewsVo newsVo = new NewsVo();

                    //评论人头像
                    List<AppUser> likesUserList = new ArrayList<AppUser>();
                    List<Likes> likesList = likesDao.getLikesByNewsId2(news.getId());

                    for (Likes likes1:likesList){
                        AppUser appUser = appUserDao.getUserById(likes1.getMyId());
                        if (appUser!=null){
                            likesUserList.add(appUser);
                        }
                    }

                    AppUser appUser = appUserDao.getUserById(news.getAppUserId());
                    if (appUser!=null){
                        List<Image> imageList = imageDao.getImageByNewsId(newsId);
                        newsVo.setImageList(imageList);
                        newsVo.setId(news.getId());
                        newsVo.setAppUserId(news.getAppUserId());
                        newsVo.setLikesUserList(likesUserList);
                        newsVo.setHeadImageCount(likesUserList.size());
                        newsVo.setContent(news.getContent());
                        Focus focus = focusDao.getFocusByIds(myId, news.getAppUserId());
                        if (focus != null) {
                            appUser.setIsFocus(1);
                        } else {
                            appUser.setIsFocus(0);
                        }
                        if (likesDao.getLikesByNewsId(myId, news.getId()) != null) {
                            newsVo.setIsLikes(1);
                            appUser.setIsLikes(1);
                        } else {
                            newsVo.setIsLikes(0);
                            appUser.setIsLikes(0);
                        }

                        newsVo.setAppUser(appUserDao.getUserById(news.getAppUserId()));
                        newsVo.setAuthorization(news.getAuthorization());
                        newsVo.setCreateTime(news.getCreateTime());
                        newsVo.setUpdateTime(news.getUpdateTime());

                        List<Comment> commentList2 = commentDao.getCommentByNewsId2(newsId);
                        for (Comment comment : commentList2) {
                            comment.setAppUserImageHead(appUserDao.getUserById(comment.getMyId()).getHeadImage());
                            comment.setMyName(appUserDao.getUserById(comment.getMyId()).getNickname());
                            if (comment.getToId() != 0) {
                                comment.setToName(appUserDao.getUserById(comment.getToId()).getNickname());
                            }
                        }
                        newsVo.setCommentCount(commentDao.getCommentByNewsIdCount(news.getId()));
                        newsVo.setCommentList(commentList2);
                        resultMap.put("newsVo", newsVo);
                    }else {
                        resultMap.put("msg","发布人信息不详");
                    }
                }

                resultMap.put("result", true);

            } else {
                resultMap.put("msg", "您已经赞过此动态，请不要重复点赞");
                resultMap.put("result", false);
            }


        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }
}
